/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ortus.daemon;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author jphipps
 */
public class cronItem extends ortus.ortusvars {
	long run_interval;
	String run_method;
	Object[] run_parms = null;
	long last_run = new Date().getTime();

	public cronItem(long run_interval, String run_method, Object[] run_parms) {
		this.run_interval = run_interval;
		this.run_method = run_method;
		this.run_parms = run_parms;
		last_run = new Date().getTime() - 2 * run_interval * 1000;
	}

	public String toString() {
		StringBuffer message = new StringBuffer();
		message.append( "cronItem:" );
		message.append( "\nInterval = " + run_interval );
		message.append( "\nMethod = " + run_method );
		message.append( "\n" );
		return message.toString();
	}
	
	public long GetLastRun() {
		return last_run;
	}

	public void SetJustRun() {
		last_run = new Date().getTime();
	}

	public boolean IsNeedToBeRun() {
		long next_run = last_run + run_interval*1000;
//		DebugLog(TRACE2,"IsNeedToBeRun: comparing next run: " + next_run + " to current: " + new Date().getTime());
		if ( next_run < new Date().getTime())
			return true;
		else
			return false;
	}
	
	public Object[] GetCmd() {
		List<Object> rcmd = new ArrayList<Object>();
		rcmd.add(run_method);
		for ( Object x : run_parms)
			rcmd.add(x);
		return rcmd.toArray();
	}
}
